package com.adminkk.runner;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.adminkk.mapper.AuthorMapper;
import com.adminkk.mapper.MusicMapper;
import com.adminkk.mapper.VideoMapper;
import com.adminkk.spider.SpiderThread;
import com.adminkk.web.TestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class SpiderCommandLineRunner  implements CommandLineRunner {

    static Logger logger = LoggerFactory.getLogger(SpiderCommandLineRunner.class);
    @Autowired
    private AuthorMapper authorMapper;
    @Autowired
    private MusicMapper musicMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Override
    public void run(String... strings) throws Exception {

        logger.error("开始抓取数据……");

        SpiderThread.absolutePath = FileUtil.getAbsolutePath(new File("D:\\test"));
        SpiderThread.absolutePath = SpiderThread.absolutePath + File.separator + "下载到的视频" + File.separator;
        logger.error("下载到的视频存放路径为： {}" , SpiderThread.absolutePath);

        //多线程下载
        int availableProcessors = Runtime.getRuntime().availableProcessors();
        logger.error("availableProcessors： {}" , availableProcessors);
        for (int i = 0; i < availableProcessors ; i++) {
            ThreadUtil.execute(new SpiderThread(authorMapper,musicMapper,videoMapper));
        }




    }
}
